package com.echo.http.model;

import android.util.SparseArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * PictureArray类
 */
public class PictureArray {

    private static final String PICTURE_ID = "picture_id";
    private static final String PICTURE_URL = "picture_url";
    private static final String PICTURE_TEXT = "picture_text";

    public class Picture{
        private String id;
        private String url;
        private String text;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }
    }

    private SparseArray<Picture> pictureSparseArray;
    private JSONArray jsonArray;

    public PictureArray(JSONArray jsonArray){
        this.jsonArray = jsonArray;
    }

    public SparseArray<Picture> getPictureSparseArray(){
        pictureSparseArray = new SparseArray<>();
        for (int i = 0; i < jsonArray.length(); i++) {
            Picture picture = new Picture();
            try {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String key = jsonObject.getString(PICTURE_ID);
                picture.setId(key);
                picture.setUrl(jsonObject.getString(PICTURE_URL));
                picture.setText(jsonObject.getString(PICTURE_TEXT));
                pictureSparseArray.put(Integer.valueOf(key), picture);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return pictureSparseArray;
    }

}
